package com.bootdo.collector.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.ParseException;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.protocol.RequestAuthCache;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClient {

   private  static  String imcUserName = "admin";
   private static String imcPassword ="admin@123";
   private static String  imcIp ="11.11.210.145";
   private static int  imcPort =8080;



   private static final HttpClientContext context() {

       HttpHost targetHost = new HttpHost(imcIp, imcPort, "http");
       CredentialsProvider credsProvider = (CredentialsProvider) new BasicCredentialsProvider();
       credsProvider.setCredentials(
               new AuthScope(imcIp, imcPort),
               new UsernamePasswordCredentials(imcUserName, imcPassword));

       // Create AuthCache instance
       AuthCache authCache = new BasicAuthCache();
       // Generate BASIC scheme object and add it to the local auth cache
       BasicScheme basicAuth = new BasicScheme();
       authCache.put(targetHost, basicAuth);

       // Add AuthCache to the execution context
       HttpClientContext context = HttpClientContext.create();
       context.setCredentialsProvider(credsProvider);
       context.setAuthCache(authCache);
       return context;
   }

   public static final CloseableHttpClient getClinet() {
       CloseableHttpClient  httpclient= HttpClients.createDefault();

       return httpclient;
   }

    public static  String get(String url) {



        CloseableHttpClient  httpclient = getClinet();

        try {
            // 创建httpget.
            HttpGet httpget = new HttpGet(url);
            RequestAuthCache raca = new RequestAuthCache();

            System.out.println("executing request " + httpget.getURI());
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget,context());
            try {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                System.out.println("--------------------------------------");
                // 打印响应状态
                //System.out.println(response.getStatusLine());
                if (entity != null) {
                    // 打印响应内容长度
                    // System.out.println("Response content length: " + entity.getContentLength());
                    // 打印响应内容
                    String res = EntityUtils.toString(entity);
                    System.out.println("Response content: " + res);
                    return res;
                }

            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

     public static  String  urlformat (String url) {
       return   String.format(url,imcIp,imcPort,System.currentTimeMillis());
     }

    public static  String  urlformat2 (String url) {
        long now = System.currentTimeMillis()/1000;
        long f = now - 120;
        System.out.println(System.currentTimeMillis());
        return   String.format(url,imcIp,imcPort, f, now,f ,now ,System.currentTimeMillis());
    }
}